package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert(" insert into orders\n" +
            "        (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,\n" +
            "         phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number,\n" +
            "         tableware_status)\n" +
            "        values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    void saveOrder(Orders orders);



    Page<OrderVO> historyOrders(Long userId, Integer status);

    @Select("select * from orders where id = #{id}")
    OrderVO getOrderDetailByOrderId(Long id);

    @Update("update orders set status = 6 where id = #{id}")
    void cancelOrder(Long id);

    Page<OrderVO> conditionSearch(LocalDate beginTime, LocalDate endTime, Integer number, String phone, Integer status);

    @Select("select count(*) from orders where status = #{status}")
    Integer getNumberByStatus(Integer status);

    void updateOrderStatus(Orders orders);

    void payment(Orders orders);

    Double sumByDate(Integer status, LocalDateTime beginTime, LocalDateTime endTime);

    Integer getUserNumberByDate(LocalDateTime everydayBegin, LocalDateTime everydayEnd);

    Integer getNewUserNumberByDate(LocalDateTime everydayBegin, LocalDateTime everydayEnd);


    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getOrdersByStatusAndOrdertime(Integer status, LocalDateTime time);

    void update(Orders orders);

    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);
}
